{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://plot.ly/python/normality-test/ 注意: 原文有些对p-value的理解是错误的\n",
    "\n",
    "import plotly as py\n",
    "import plotly.graph_objs as go\n",
    "import plotly.figure_factory as ff\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import scipy\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\", message=\"numpy.dtype size changed\")\n",
    "warnings.filterwarnings(\"ignore\", message=\"numpy.ufunc size changed\")\n",
    "\n",
    "py.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wind_speed_df = pd.read_csv('./wind_speed_laurel_nebraska.csv')\n",
    "\n",
    "wind_speed_df[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = ff.create_table(wind_speed_df[0:10])\n",
    "py.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "avg_ss = wind_speed_df['10 Min Sampled Avg']\n",
    "\n",
    "hist_trace = go.Histogram(\n",
    "    x = avg_ss,\n",
    "    autobinx = True, \n",
    "    marker = dict(\n",
    "        color = '#00FF00'\n",
    "    ),\n",
    ")\n",
    "\n",
    "layout = go.Layout(\n",
    "    width = 600,\n",
    "    title = 'Hist Fig',\n",
    ")\n",
    "\n",
    "hist_fig = go.Figure(data=[hist_trace], layout=layout)\n",
    "py.offline.iplot(hist_fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 夏皮罗-威尔克 shapiro-wilk\n",
    "\n",
    "sw_result_tup = scipy.stats.shapiro(x=avg_ss)\n",
    "print(sw_result_tup)\n",
    "\n",
    "sw_list = [\n",
    "    ['', 'DF', 'W-Test', 'P-Value'],\n",
    "    ['Sample Data', len(avg_ss) + 1, sw_result_tup[0], sw_result_tup[1]],\n",
    "]\n",
    "\n",
    "sw_tabel_fig = ff.create_table(table_text=sw_list)\n",
    "py.offline.iplot(sw_tabel_fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Kolmogorov-Smirnov test 柯尔莫哥洛夫-斯米尔诺夫检验\n",
    "# avg_ss = np.random.normal(size=1000)\n",
    "ks_result_tup = scipy.stats.kstest(rvs=avg_ss, cdf='norm')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "anderson_results = scipy.stats.anderson(avg_ss)\n",
    "print(anderson_results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dagostino_results = scipy.stats.mstats.normaltest(a=avg_ss)\n",
    "dagostino_results"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
